home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / aspisrc.zip / EXTRACT.C < prev    next >
C/C++ Source or Header  |  1992-01-26  |  21KB  |  744 lines

  1. /* Extract files from a tar archive.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * Extract files from a tar archive.
  22.  *
  23.  * Written 19 Nov 1985 by John Gilmore, ihnp4!hoptoad!gnu.
  24.  *
  25.  * @(#) extract.c 1.32 87/11/11 - gnu
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <errno.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32.  
  33. #ifdef BSD42
  34. #include <sys/file.h>
  35. #endif
  36.  
  37. #ifdef USG
  38. #include <fcntl.h>
  39. #endif
  40.  
  41. #ifdef    MSDOS
  42. #include <fcntl.h>
  43. #endif    /* MSDOS */
  44.  
  45. /*
  46.  * Some people don't have a #define for these.
  47.  */
  48. #ifndef    O_BINARY
  49. #define    O_BINARY    0
  50. #endif
  51. #ifndef O_NDELAY
  52. #define    O_NDELAY    0
  53. #endif
  54.  
  55. #ifdef NO_OPEN3
  56. /* We need the #define's even though we don't use them. */
  57. #include "open3.h"
  58. #endif
  59.  
  60. #ifdef EMUL_OPEN3
  61. /* Simulated 3-argument open for systems that don't have it */
  62. #include "open3.h"
  63. #endif
  64.  
  65. extern int errno;            /* From libc.a */
  66. extern time_t time();            /* From libc.a */
  67. extern char *index();            /* From libc.a or port.c */
  68.  
  69. #include "tar.h"
  70. #include "port.h"
  71.  
  72. extern FILE *msg_file;
  73.  
  74. extern union record *head;        /* Points to current tape header */
  75. extern struct stat hstat;        /* Stat struct corresponding */
  76. extern int head_standard;        /* Tape header is in ANSI format */
  77.  
  78. extern char *save_name;
  79. extern long save_totsize;
  80. extern long save_sizeleft;
  81.  
  82. extern void print_header();
  83. extern void skip_file();
  84. extern void skip_extended_headers();
  85. extern void pr_mkdir();
  86.  
  87. int make_dirs();            /* Makes required directories */
  88.  
  89. static time_t now = 0;            /* Current time */
  90. static we_are_root = 0;            /* True if our effective uid == 0 */
  91. static int notumask = ~0;        /* Masks out bits user doesn't want */
  92.  
  93. /*
  94.  * "Scratch" space to store the information about a sparse file before
  95.  * writing the info into the header or extended header
  96.  */
  97. /*struct sp_array    *sparsearray;*/
  98.  
  99. /* number of elts storable in the sparsearray */
  100. /*int    sp_array_size = 10;*/
  101.  
  102. /*
  103.  * Set up to extract files.
  104.  */
  105. extr_init()
  106. {
  107.     int ourmask;
  108.  
  109.     now = time((time_t *)0);
  110.     if (geteuid() == 0)
  111.         we_are_root = 1;
  112.  
  113.     /*
  114.      * We need to know our umask.  But if f_use_protection is set,
  115.      * leave our kernel umask at 0, and our "notumask" at ~0.
  116.      */
  117.     ourmask = umask(0);        /* Read it */
  118.     if (!f_use_protection) {
  119.         (void) umask (ourmask);    /* Set it back how it was */
  120.         notumask = ~ourmask;    /* Make umask override permissions */
  121.     }
  122. }
  123.  
  124.  
  125. /*
  126.  * Extract a file from the archive.
  127.  */
  128. void
  129. extract_archive()
  130. {
  131.     register char *data;
  132.     int fd, check, namelen, written, openflag;
  133.     long size;
  134.     time_t acc_upd_times[2];
  135.     register int skipcrud;
  136.     register int i;
  137.     int sparse_ind = 0;
  138.     union record *exhdr;    
  139.     int end_nulls;
  140.     
  141.     saverec(&head);            /* Make sure it sticks around */
  142.     userec(head);            /* And go past it in the archive */
  143.     decode_header(head, &hstat, &head_standard, 1);    /* Snarf fields */
  144.  
  145.     if(f_confirm && !confirm("extract",head->header.name)) {
  146.         if (head->header.isextended)
  147.             skip_extended_headers();
  148.         skip_file((long)hstat.st_size);
  149.         saverec((union record **)0);
  150.         return;
  151.     }
  152.  
  153.     /* Print the record from 'head' and 'hstat' */
  154.     if (f_verbose)
  155.         print_header();
  156.  
  157.     /*
  158.      * Check for fully specified pathnames and other atrocities.
  159.      *
  160.      * Note, we can't just make a pointer to the new file name,
  161.      * since saverec() might move the header and adjust "head".
  162.      * We have to start from "head" every time we want to touch
  163.      * the header record.
  164.      */
  165.     skipcrud = 0;
  166.     while (!f_absolute_paths && '/' == head->header.name[skipcrud]) {
  167.         static int warned_once = 0;
  168.  
  169.         skipcrud++;    /* Force relative path */
  170.         if (!warned_once++) {
  171.             msg("Removing leading / from absolute path names in the archive.");
  172.         }
  173.     }
  174.  
  175.     switch (head->header.linkflag) {
  176.  
  177.     default:
  178.         msg("Unknown file type '%c' for %s, extracted as normal file",
  179.             head->header.linkflag, skipcrud+head->header.name);
  180.         /* FALL THRU */
  181.  
  182.     /* 
  183.      * JK - What we want to do if the file is sparse is loop through
  184.      * the array of sparse structures in the header and read in
  185.      * and translate the character strings representing  1) the offset
  186.      * at which to write and 2) how many bytes to write into numbers,
  187.      * which we store into the scratch array, "sparsearray".  This
  188.      * array makes our life easier the same way it did in creating
  189.      * the tar file that had to deal with a sparse file.
  190.      *
  191.      * After we read in the first five (at most) sparse structures,
  192.      * we check to see if the file has an extended header, i.e., 
  193.      * if more sparse structures are needed to describe the contents
  194.      * of the new file.  If so, we read in the extended headers
  195.      * and continue to store their contents into the sparsearray.
  196.      */
  197.     case LF_SPARSE:
  198.         sp_array_size = 10;
  199.         sparsearray = (struct sp_array *) malloc(sp_array_size * sizeof(struct sp_array));
  200.         for (i = 0; i < SPARSE_IN_HDR; i++) {
  201.             sparsearray[i].offset = 
  202.                 from_oct(1+12, head->header.sp[i].offset);
  203.             sparsearray[i].numbytes = 
  204.                 from_oct(1+12, head->header.sp[i].numbytes);
  205.             if (!sparsearray[i].numbytes)
  206.                 break;
  207.         }
  208.         
  209. /*        end_nulls = from_oct(1+12, head->header.ending_blanks);*/
  210.         
  211.         if (head->header.isextended) {
  212.             /* read in the list of extended headers
  213.                and translate them into the sparsearray 
  214.                as before */
  215.  
  216.             /* static */ int ind = SPARSE_IN_HDR;
  217.             
  218.             for (;;) {
  219.                 
  220.                 exhdr = findrec();
  221.                 for (i = 0; i < SPARSE_EXT_HDR; i++) {
  222.                     
  223.                     if (i+ind > sp_array_size-1) {
  224.                     /*
  225.                      * realloc the scratch area
  226.                      * since we've run out of room --
  227.                       */
  228.                         sparsearray = (struct sp_array *) 
  229.                                 realloc(sparsearray,
  230.                                  2 * sp_array_size * (sizeof(struct sp_array)));
  231.                         sp_array_size *= 2;
  232.                     }
  233.                     if (!exhdr->ext_hdr.sp[i].numbytes)
  234.                         break;
  235.                     sparsearray[i+ind].offset = 
  236.                         from_oct(1+12, exhdr->ext_hdr.sp[i].offset);
  237.                     sparsearray[i+ind].numbytes = 
  238.                         from_oct(1+12, exhdr->ext_hdr.sp[i].numbytes);
  239.                 }
  240.                 if (!exhdr->ext_hdr.isextended) 
  241.                     break;
  242.                 else {
  243.                     ind += SPARSE_EXT_HDR;
  244.                     userec(exhdr);
  245.                 }
  246.             }
  247.             userec(exhdr);
  248.         }
  249.         
  250.         /* FALL THRU */
  251.     case LF_OLDNORMAL:
  252.     case LF_NORMAL:
  253.     case LF_CONTIG:
  254.         /*
  255.          * Appears to be a file.
  256.          * See if it's really a directory.
  257.          */
  258.         namelen = strlen(skipcrud+head->header.name)-1;
  259.         if (head->header.name[skipcrud+namelen] == '/')
  260.             goto really_dir;
  261.  
  262.         /* FIXME, deal with protection issues */
  263.     again_file:
  264.         openflag = (f_keep?
  265.             O_BINARY|O_NDELAY|O_WRONLY|O_CREAT|O_EXCL:
  266.             O_BINARY|O_NDELAY|O_WRONLY|O_CREAT|O_TRUNC)
  267.             | ((head->header.linkflag == LF_SPARSE) ? 0 : O_APPEND);            
  268.             /*
  269.              * JK - The last | is a kludge to solve the problem
  270.              * the O_APPEND flag  causes with files we are
  271.              * trying to make sparse:  when a file is opened
  272.              * with O_APPEND, it writes  to the last place
  273.              * that something was written, thereby ignoring
  274.              * any lseeks that we have done.  We add this
  275.              * extra condition to make it able to lseek when
  276.              * a file is sparse, i.e., we don't open the new
  277.              * file with this flag.  (Grump -- this bug caused
  278.              * me to waste a good deal of time, I might add)
  279.                */
  280.  
  281.         if(f_exstdout) {
  282.             fd = 1;
  283.             goto extract_file;
  284.         }
  285. #ifdef O_CTG
  286.         /*
  287.          * Contiguous files (on the Masscomp) have to specify
  288.          * the size in the open call that creates them.
  289.          */
  290.         if (head->header.linkflag == LF_CONTIG)
  291.             fd = open(skipcrud+head->header.name, openflag | O_CTG,